package com.usermanage.controller;

import com.usermanage.Service.OperationService;
import com.usermanage.beanParam.OperationBean;
import com.usermanage.util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 *一元操作接口
 */
@Api(description = "一元操作接口")
@RestController
@RequestMapping("/operationDetail")
public class OperationDetailController {

    @Resource
    private OperationService operationService;



    @ApiOperation(value = "根据合作细节，查找一元操作详情" ,  notes="根据合作细节，查找一元操作详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "组id", required = true,  paramType = "query", dataType = "String")
    })
    @RequestMapping(value="/searchOperationDetail",method= RequestMethod.GET)
    public JsonData searchOperationDetailList(@RequestParam("groupId") String groupId)throws Exception{
        //获取组织下一元操作列表
        List list = operationService.searchOperation(groupId);
        return JsonData.success(list);
    }


    @ApiOperation(value = "根据合作细节id添加一元操作" ,  notes="根据合作细节id添加一元操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "operationDetailId", value = "操作详情id", required = true,  paramType = "query", dataType = "String")
    })
    @RequestMapping(value="/creatOperation",method= RequestMethod.POST)
    public JsonData creatOperation(@Valid @RequestBody OperationBean operationBean, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return JsonData.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        //验证一元操作是否存在
        Boolean bool = operationService.isExistOperation("");
        if (bool){
            return JsonData.fail("一元操作已存在");
        }
        //创建一个一元操作
        int result = operationService.creatOperation(operationBean);
        if (result == 0){
            return JsonData.fail("一元操作创建失败，系统异常！");
        }
        return JsonData.success();
    }

}
